package locations.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import locations.dao.DAOFactory;
import locations.dao.InstitutionResponsibleDAO;
import locations.model.InstitutionResponsible;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class SaveInstitutionResponsibleAction extends ActionSupport 
        implements ModelDriven<InstitutionResponsible>, ServletRequestAware, ServletResponseAware
{

    private static final long serialVersionUID = -8124967730231198191L;
	
    private HttpServletRequest request; 
    private HttpServletResponse response; 
    private InstitutionResponsible institutionResponsible = new InstitutionResponsible();

    public String execute()
    {
        InstitutionResponsibleDAO dao = DAOFactory.getInstitutionResponsibleDAO();
        dao.save(this.institutionResponsible);
        return SUCCESS;
    }

    public HttpServletRequest getRequest()
    {
        return request;
    }

    public InstitutionResponsible getInstitutionResponsible()
    {
        return institutionResponsible;
    }

    public void setInstitutionResponsible(InstitutionResponsible institutionResponsible)
    {
        this.institutionResponsible = institutionResponsible;
    }

    @Override
    public InstitutionResponsible getModel()
    {
        return this.institutionResponsible;
    }

    public void setRequest(HttpServletRequest request)
    {
        this.request = request;
    }

    public void setResponse(HttpServletResponse response)
    {
        this.response = response;
    }

    public HttpServletResponse getResponse()
    {
        return this.response;
    }

    @Override
    public void setServletResponse(HttpServletResponse response)
    {
        this.response = response;
    }

    @Override
    public void setServletRequest(HttpServletRequest request)
    {
        this.request = request;
    }
    
    public void validate()
        {
            boolean ok = true;
            String name = this.getInstitutionResponsible().getNombre();
            String lastName = this.getInstitutionResponsible().getApellido();
            if (name.equals(""))
            {
                addFieldError("nombre", "El nombre no puede estar en blanco");
                    ok = false;
            }
            if (lastName.equals(""))
            {
                addFieldError("apellido", "El apellido no puede estar en blanco");
                    ok = false;
            }
            if (ok)
            {
                InstitutionResponsibleDAO dao = DAOFactory.getInstitutionResponsibleDAO();
                long auxId = (this.getInstitutionResponsible().getId() == null? 0 : this.getInstitutionResponsible().getId());
                if (dao.exists(name, lastName, auxId))
                {
                    addFieldError("nombre", "Ya existe un responsable de institucion con ese nombre y apellido");
                    ok = false;
                }
            }
        }
}
